import config from 'config.js'
import {Request} from 'request.js'



  // 广告
  // 在页面中定义激励视频广告
  function loadVideoAd(app) {
    // 用户触发广告后，显示激励视频广告
    if (app.videoAd) {
      app.videoAd.show().catch(() => {
        // 失败重试
        app.videoAd.load()
          .then(() => app.videoAd.show())
          .catch(err => {
            console.log('激励视频 广告显示失败')
          })
      })
    }
  }



  function createInsertAd(app, adId, callBack = function(res) {}) {
    console.log("-------0000------",adId);
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      app.interstitialAd = wx.createInterstitialAd({
        adUnitId: adId
      })
      app.interstitialAd.onLoad(() => {
        if (app.interstitialAd) {
          app.interstitialAd.show().catch((err) => {
            console.error(err)
          })
        }
        callBack();
      })
      app.interstitialAd.onError((err) => {
        console.log("-------2222------",err);
      })
      app.interstitialAd.onClose(() => {
        console.log("-------00333300------",adId);
      })
    }
  }
  
  function createAd (app, adId, callBack = function(res) {}) {
    console.log("-------0000------",adId);
    // 在页面onLoad回调事件中创建激励视频广告实例
    if (wx.createRewardedVideoAd) {
      app.videoAd = wx.createRewardedVideoAd({
        adUnitId: adId
      })
      app.videoAd.onLoad(() => {
        console.log("-------111------");
      })
      app.videoAd.onError((err) => {
        console.log("-------2222------");
      })
      app.videoAd.onClose((res) => {
        console.log("-------333------",res);
        if(res.isEnded) {
          callBack();
        }
      })
    }
  }



 // 上传图片
 function wxUpLoad(imgPath) {
  let APIURL = config.BASE_API + 'lottery/upload';
  let _header = {
    // 'content-type': 'application/json' // 默认值
    'content-type': 'application/x-www-form-urlencode' // 默认值
  };

  return new Promise(function (resolve, reject){
    let authorization = wx.getStorageSync('authorization');
    if(authorization) {
      _header.Authorization = authorization;
    }
    wx.showLoading({
      title: '上传中...',
    })
    
    wx.uploadFile({
      url: APIURL,
      filePath: imgPath,
      name: 'image',
      header: _header,
      formData: {
        // image: imgPath
      },
      success (res){

        if (res.statusCode === 200) {
          var jsondata = JSON.parse(res.data);
          if(jsondata.code == 0) {
            resolve(jsondata);
          }else {
            // resolve(jsondata);
            wx.showToast({title: res.data.msg, icon: 'none',duration: 1500});
          }
        }else {
            wx.showToast({title: "网络异常",icon: 'none',duration: 1500 });
            resolve(jsondata);
        }

        wx.hideLoading();
      }, 
      fail: function(e) {
        wx.showToast({title: "网络异常",icon: 'none',duration: 1500 });
        wx.hideLoading();
        reject(e)
      }
    })


  });
}



function logTask(id, callBack = function(res) {}) {
  let that = this;
  let param = {
    url: 'task/log',
    method: 'POST',
    data: {
      id: id,
    }
  }
  Request(param).then(function(res){
    callBack();
  });

}



/**
 * 插入消息订阅模板
 * {tmpl_id, sence} param 
 */
function subscriptionsPost(data, callBack = function(res) {}) {
  let that = this;
  let param = {
    url: 'user/subscriptionsPost',
    method: 'POST',
    data: data
  }
  Request(param).then(function(res){
    callBack(res);
  });
}


module.exports = {
  wxUpLoad: wxUpLoad,
  createAd: createAd,
  createInsertAd: createInsertAd,
  loadVideoAd: loadVideoAd,
  logTask: logTask,
  subscriptionsPost: subscriptionsPost
}
